package me.j360.springboot.simple.web;

import me.j360.springboot.simple.bean.config.ConfigurationPropertiesConfig;
import me.j360.springboot.simple.bean.config.FooComponent;
import me.j360.springboot.simple.bean.config.SystemConfig;
import me.j360.springboot.simple.dao.AdminDao;
import me.j360.springboot.simple.entity.Admin;
import me.j360.springboot.simple.entity.User;
import me.j360.springboot.simple.service.activemq.Producer;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Scope;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.PersistenceContext;
import javax.sql.DataSource;
import javax.transaction.Transactional;
import java.sql.SQLException;
import java.util.Date;
import java.util.Map;

/**
 * Created with springbootweb -> me.j360.springboot.simple.web.
 * User: min_xu
 * Date: 2015/7/29
 * Time: 10:18
 * 说明：
 */

@Controller
public class UserController {

    @Autowired
    private JdbcTemplate jdbcTemplate;
    @Autowired
    private NamedParameterJdbcTemplate namedParameterJdbcTemplate;
    @Autowired
    private SystemConfig systemConfig;
    @Autowired
    private ConfigurationPropertiesConfig configurationPropertiesConfig;

    @Autowired
    private SessionFactory sessionFactory;
    @Autowired
    private DataSource dataSource;
    @Autowired
    private Producer producer;

    @Autowired
    private EntityManagerFactory entityManagerFactory;
    @Autowired
    private AdminDao adminDao;

    @RequestMapping("/list")
    String list() {
        System.out.println(systemConfig.getName() + " "+ configurationPropertiesConfig.getUsername());

        //

        /*Session session = sessionFactory.openSession();
        User user = new User("徐敏");
        session.save(user);
        sessionFactory.close();*/

        User user = new User("徐敏11");
        entityManagerFactory.createEntityManager().persist(user);

        return "Hello World!";
    }


    @RequestMapping("/login")
    String login(Model model) {
        User user = new User("徐敏11");
        user.setId(100000L);
        model.addAttribute("user",user);

        this.producer.send("Test message");

        Admin admin = new Admin();
        admin.setIsAccountEnabled(true);
        admin.setIsAccountExpired(true);
        admin.setIsCredentialsExpired(false);
        admin.setIsAccountLocked(true);
        admin.setUsername("aaa");
        admin.setPassword("2234");
        admin.setSalt("adfadf");

        adminDao.save(admin);
        return "login";
    }


    @RequestMapping("/index")
    String index(Model model) {
        User user = new User("徐敏11");
        user.setId(100000L);
        model.addAttribute("user",user);

        return "index";
    }
    @RequestMapping("/admin")
    String admin(Model model) {
        User user = new User("徐敏11");
        user.setId(100000L);
        model.addAttribute("user",user);

        return "admin";
    }

}
